<template>
  <d2-container>

    <template slot="header">

      <el-form :gutter="24" ref="search-box" :model="mainQuery"  label-position="left" :inline="true" label-width="90px" size="mini">

          <el-row>

            <el-col :span="8">
              <el-form-item label="商户号" >
                <el-input placeholder="" v-model="mainQuery.mchNo" @keyup.enter.native="handleSearch"  clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="网址" >
                <el-input placeholder="" v-model="mainQuery.url" @keyup.enter.native="handleSearch"  clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="审批状态">
                <el-select v-model="mainQuery.reviewStatus" clearable @change="handleSearch">
                  <el-option v-for="item in reviewStatusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>

          <el-form-item label="申请时间" >
            <el-date-picker :picker-options="datetimePickerOptions" v-model="mainQuery.createTimeRange" @change="handleSearch" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
          </el-form-item>

          <el-form-item label="审批时间" >
            <el-date-picker :picker-options="datetimePickerOptions" v-model="mainQuery.reviewTimeRange" @change="handleSearch" type="daterange" value-format="yyyy-MM-dd HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>

      </el-form>

      <div class="search-btn-box">

        <div class="left">
          <el-button-group>
            <el-button size="mini" class="filter-item" icon="el-icon-plus" type="success" @click="handleAdd" :loading="mainTable.loading" >添加</el-button>
            <!-- <el-button class="filter-item" icon="el-icon-download" size="mini" @click="handleExportExcel">导出</el-button> -->
          </el-button-group>

          <el-button-group v-if="$options.filters['checkButtonAccess']('tradeorderfake-upload')" style="margin-left: 30px;">
            <el-button class="filter-item" icon="el-icon-upload" type="plain" @click="handleUpload" :loading="mainTable.loading" size="mini">批量上传添加</el-button>
            <el-button class="filter-item" icon="el-icon-download" type="plain" @click="handleDownloadTemplate" :loading="mainTable.loading" size="mini">下载模板</el-button>
          </el-button-group>

        </div>
        <div class="right">
          <el-button size="mini" class="filter-item" icon="el-icon-refresh" type="default" @click="resetMainData" :loading="mainTable.loading" >重置</el-button>
          <el-button size="mini" class="filter-item" icon="el-icon-search" type="primary" @click="handleSearch" :loading="mainTable.loading" >搜索</el-button>
        </div>
      </div>

    </template>

    <!--主列表数据-->
    <d2-crud
      ref="d2Crud"
      :selection-row="mainQuery.allowSelect"
      :options="mainTable.options"
      :index-row="mainTable.indexRow"
      :columns="mainTable.columns"
      :data="mainTable.list"
      :loading="mainTable.loading"
      :rowHandle="mainTable.rowHandle"
      @current-change="onMainCurrentChange"
      @selection-change="onMainSelectionChange"

      @handle-set="handleSet"
      @handle-del="handleDel"
    ></d2-crud>

    <template slot="footer">
      <el-pagination
        @size-change="handleMainSizeChange"
        @current-change="handleMainPageChange"
        :current-page="mainTable.pagination.currentPage"
        :page-sizes="mainTable.pagination.pageSizes"
        :page-size="mainTable.pagination.pageSize"
        :page-count="mainTable.pagination.pageCount"
        :total="mainTable.pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        :background="true"
        >
      </el-pagination>
    </template>

    <dialog-add ref="dialog-add" @dialog-close="fetchMainData"></dialog-add>

    <dialog-upload ref="dialog-upload" @dialog-close="fetchMainData"></dialog-upload>

    <common-export ref="common-export"></common-export>

  </d2-container>
</template>

<script>

import ApiMain from '@/api/trade.website';
import util from '@/libs/util.js';
import { reviewStatusOptions } from '@/const/constant';
import DialogAdd from './dialog-add';
import DialogUpload from './dialog-upload';
import CommonExport from '../../common/export';
import DatetimePickerMixin from '@/mixin/DatetimePickerMixin'


export default {
  name: 'mch-website',
  mixins: [
    DatetimePickerMixin,
  ],
  components: {
    DialogAdd,
    DialogUpload,
    'common-export': CommonExport,
  },
  data () {
    return {

      reviewStatusOptions,

      mainQueryRaw: null,
      mainQuery: {
        mchNo          : undefined,
        url            : undefined,
        reviewStatus   : undefined,
        reviewTimeRange: undefined,
        createTimeRange: undefined,
      },

      mainTable: {
        loading: false,
        options: {
          border             : true,
          stripe             : true,
          highlightCurrentRow: true,
          emptyText          : '暂无数据',
          size               : 'mini',
        },
        // indexRow: {
        //   title:'序号',
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          {title: '商户号', key: 'mchNo', align: 'center', width: '180', showOverflowTooltip: true, },
          {title: '网址', key: 'url', align: 'center', width: '180', showOverflowTooltip: true},
           {title: '备案号', key: 'beianNo', align: 'center', width: '180', showOverflowTooltip: true},
          {title: '审批状态', key: 'strReviewStatus', align: 'center', width: '180', showOverflowTooltip: true, },
          {title: '申请人', key: 'createBy', align: 'center', width: '180', showOverflowTooltip: true},
          {title: '申请时间', key: 'createTime', align: 'center', width: '180', showOverflowTooltip: true},
          {title: '审批时间', key: 'reviewTime', align: 'center', width: '180', showOverflowTooltip: true},
        ],
        rowHandle: {
          width: '160',
          align: 'center',
          fixed: 'right',
          custom: [
            {
              text: '修改',
              type: 'primary',
              size: 'mini',
              emit: 'handle-set'
            },
            {
              text: '删除',
              type: 'danger',
              size: 'mini',
              plain: true,
              emit: 'handle-del'
            }
          ]
        },
        list: [],
        pagination: {
          currentPage: 1,
          pageSize   : 50,
          total      : 0,
          pageSizes  : [50, 100, 200]
        },
        selectionRow: [],
      },
    }
  },
  mounted () {
    this.mainQueryRaw = Object.copy(this.mainQuery);

    this.fetchOptions().finally(() => {
      this.resetMainData();
    });
  },
  beforeDestroy () {
  },
  methods: {

    renderMainData (re) {
      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleSearch () {
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData();
    },
    handleMainSizeChange (val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange (val) {
      this.mainTable.pagination.currentPage = val;
      this.fetchMainData()
    },

    buildMainQuery () {

      var query = {
        page  : this.mainTable.pagination.currentPage,
        limit : this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return Object.copy(query);
    },

    resetMainData () {

      this.mainQuery = Object.copy(this.mainQueryRaw);

      this.fetchMainData();
    },

    async fetchOptions () {

    },

    async fetchMainData () {

      this.mainTable.loading = true;

      try {

        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {}

      this.mainTable.loading = false;

    },

    onMainCurrentChange (now, old) {
      this.mainTable.current = now;
    },

    onMainSelectionChange (now, old) {
      this.mainTable.selectionRow = now
    },

    onMainDoubleClick (now) {
      this.mainTable.current = now;
      this.handleSet();
    },

    handleAdd () {
      this.$refs['dialog-add'].show();
    },

    handleSet ({index, row}) {
      this.$refs['dialog-add'].show(Object.copy(row));
    },

    handleDel ({index, row}) {

      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        var req = {id: row.id};

        ApiMain.del(req).then(() => {
          this.$message({message: '操作成功', type: 'success'});  
          this.fetchMainData();
        });

      });
    },

    handleExportExcel () {
      this.$refs['common-export'].handleExportExcel(ApiMain, this.mainQuery, true);
    },

    handleUpload () {
      this.$refs['dialog-upload'].show();
    },
    handleDownloadTemplate () {
      window.open(util.getDownloadTemplate('MchTradeWebsite'), '_blank');
    },

  }
}
</script>

<style>

</style>
